package com.ssy.lingxi.order.config;

import com.querydsl.jpa.impl.JPAQueryFactory;
import com.ssy.lingxi.order.api.model.constant.OrderFeignConstant;
import com.ssy.lingxi.order.model.constant.OrderServiceContants;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.persistence.EntityManager;
import java.util.HashMap;
import java.util.Map;

/**
 * 订单服务配置
 * @author 万宁
 * @version 2.0.0
 * @date 2021-08-05
 */
@Configuration
public class OrderServiceConfig {

    /**
     * QueryDsl Bean 配置
     * @param entityManager Jpa的EntityManager
     * @return QueryDsl JpaQueryFactory，直接注入使用
     */
    @Bean
    public JPAQueryFactory jpaQueryFactory(EntityManager entityManager) {
        return new JPAQueryFactory(entityManager);
    }

    //*********************************** 内部延迟队列 *****************************************
    @Bean
    public CustomExchange orderAutoReceiveExchange() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-delayed-type", "direct");
        return new CustomExchange(OrderServiceContants.ORDER_SERVICE_DELAY_EXCHANGE, "x-delayed-message", true, false, args);
    }

    @Bean
    public Queue orderAutoReceiveQueue() {
        return new Queue(OrderServiceContants.ORDER_SERVICE_DELAY_QUERY, true);
    }

    @Bean
    public Binding orderAutoReceiveBinding(Queue orderAutoReceiveQueue, CustomExchange orderAutoReceiveExchange) {
        return BindingBuilder.bind(orderAutoReceiveQueue).to(orderAutoReceiveExchange).with(OrderServiceContants.ORDER_SERVICE_DELAY_ROUTING_KEY).noargs();
    }

    //*********************************** 营销服务，拼团订单拼团结果通知队列 *****************************************
    @Bean
    public DirectExchange orderGroupExchange() {
        return new DirectExchange(OrderFeignConstant.ORDER_GROUP_EXCHANGE, true, true);
    }

    @Bean
    public Queue orderGroupQueue() {
        return new Queue(OrderFeignConstant.ORDER_GROUP_QUERY, true);
    }

    @Bean
    public Binding orderGroupBinding() {
        return BindingBuilder.bind(orderGroupQueue()).to(orderGroupExchange()).with(OrderFeignConstant.ORDER_GROUP_ROUTINGKEY);
    }

    //*********************************** 支付服务，订单支付结果通知队列 *****************************************
    @Bean
    public DirectExchange orderPayCallbackExchange() {
        return new DirectExchange(OrderFeignConstant.ORDER_PAY_CALLBACK_EXCHANGE, true, true);
    }

    @Bean
    public Queue orderPayCallbackQueue() {
        return new Queue(OrderFeignConstant.ORDER_PAY_CALLBACK_QUERY, true);
    }

    @Bean
    public Binding orderPayCallbackBinding() {
        return BindingBuilder.bind(orderPayCallbackQueue()).to(orderPayCallbackExchange()).with(OrderFeignConstant.ORDER_PAY_CALLBACK_ROUTINGKEY);
    }

    //*********************************** 商品服务，物料价格信息变更队列 *****************************************
    @Bean
    public DirectExchange orderGoodsPriceChangeExchange() {
        return new DirectExchange(OrderServiceContants.ORDER_GOODS_PRICE_CHANGE_EXCHANGE, true, true);
    }

    @Bean
    public Queue orderGoodsPriceChangeQueue() {
        return new Queue(OrderServiceContants.ORDER_GOODS_PRICE_CHANGE_QUEUE, true);
    }

    @Bean
    public Binding orderGoodsPriceBinding() {
        return BindingBuilder.bind(orderGoodsPriceChangeQueue()).to(orderGoodsPriceChangeExchange()).with(OrderServiceContants.ORDER_GOODS_PRICE_CHANGE_ROUTING_KEY);
    }
}
